package com.icefish.utils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description: 用于初始化 Servlet 跨域、浏览器缓存、编码、响应格式的配置
 * Date: 2022/11/30 23:38
 * Author: 26480
 *
 * @since JDK 1.8
 */
public class Init_Req_Resp_Config {
    public  static void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 允许跨域
        response.setHeader("Access-Control-Allow-Origin", "*");
//        Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS
        response.addHeader("Access-Control-Allow-Method","PUT, POST, GET, OPTIONS");
//        Access-Control-Allow-Headers: authorization
        response.addHeader("Access-Control-Allow-Headers", "Authorization");
        response.addHeader("Access-Control-Allow-Headers", "authorization");
        // 防止浏览器不缓存该请求内容，
        // 保证每次请求该HTTP时，浏览器都会发出新请求，而不是显示缓存的内容
        response.setHeader("Cache-Control", "no-cache");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
    }
}
